import Vue from 'vue'
import Vuex from 'vuex'
import isEmpty from 'lodash/isEmpty'


Vue.use(Vuex)


export default new Vuex.Store({
	state: {

		/**
		 * 登录信息
		 */
		userInfo: {}
	},

	getters: {
		userInfo({userInfo}){
			return userInfo
		},
		loginStatus({userInfo}){
			return !isEmpty(userInfo)
		}
	},

	mutations: {
		updateUserInfo(state, info = {}){
			state.userInfo = info
		}
	},

	actions: {
		initStore({dispatch}, options){
			return dispatch('commitUserInfo', options)
		},
		commitUserInfo({commit}, {app}){
			return new Promise(resolve => {
				const cb = res => {
					commit('updateUserInfo', (res.userInfo || {}).body || {})
					resolve()
				}
				app.api.getUserInfo().then(cb, resolve)
			})
		}
	}
})